<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="学科名称" prop="subjectName">
      <el-input v-model="form.subjectName"></el-input>
    </el-form-item>
    <el-form-item label="是否显示">
      <el-switch
        v-model="displayValue"
        active-color="#13ce66"
        inactive-color="#ff4949"
      >
      </el-switch>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="hCancel">取 消</el-button>
      <el-button @click="onSubmit">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { update, add } from '@/api/hmmm/subjects'

export default {
  name: 'subDialog',
  props: {
    row: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      dialogVisible: false,
      displayValue: true,
      form: {
        subjectName: '',
        isFrontDisplay: 0
      },
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // console.log(this.row, 888888)
    this.form.subjectName = this.row.subjectName
    this.form.isFrontDisplay = this.row.isFrontDisplay
    // console.log(this.form.isFrontDisplay)
  },
  watch: {
    displayValue: {
      handler (newValue) {
        this.form.isFrontDisplay = newValue ? 1 : 0
      },
      immediate: true,
      deep: true
    },
    form: {
      handler (newValue) {
        this.displayValue = !!this.form.isFrontDisplay
      },
      immediate: true,
      deep: true
    }
  },

  methods: {
    async onSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // if (this.form.isFrontDisplay) {
          //   this.form.isFrontDisplay = 1
          // } else {
          //   this.form.isFrontDisplay = 0
          // }

          let res = {}
          if (!this.isEdit) {
            res = await add({
              ...this.form
            })
            if (res.status !== 200) return
            this.$message.success('恭喜你添加成功')
          } else {
            res = await update({
              id: this.row.id,
              ...this.form
            })
            if (res.status !== 200) return
            // console.log(res, 3452351)
            this.$message.success('恭喜你修改学科成功')
          }
          this.$emit('scucces')
        }
      })
    },
    hCancel () {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped></style>
